<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Released under the MIT License.
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------

namespace app\app\controller;

use cmf\controller\AdminBaseController;
use think\Db;
use app\admin\model\AdminMenuModel;
use think\Validate;
use api\wxapp\model\Diary;
use api\wxapp\model\User;
use api\wxapp\model\Shop;
use api\wxapp\model\Clerk;
class UserController extends AdminBaseController
{
    private $shop_id;
    private $where = array();
    public function initialize()
    {
        parent::initialize();
        $sessionAdminId = session('ADMIN_ID');
        if($sessionAdminId !== 1){
            //说明是店铺登陆
            $find = Shop::where('admin_id',$sessionAdminId)->find();
            $this->where[] = ['shop_id','eq',$find['id']];
        }
    }
    public function index()
    {
        $params = $this->param();
        if(isset($params['nickname'])){
            $this->where[] = ['nickname','like','%'.$params['nickname'].'%'];
        }
        if(isset($params['phone'])){
            $this->where[] = ['phone','like','%'.$params['phone'].'%'];
        }
        $user = User::where($this->where)->paginate(10,false,
            ['query'=>$params])->each(function($item, $key){
            if($item['shop_id']){
                $item['shopname'] = Shop::where('id',$item['shop_id'])->value('name');
            }else{
                $item['shopname'] = '无';
            }
            return $item;
        });
        $this->assign('user',$user);
        $user->appends($params);  //分页条件
        $this->assign('page', $user->render());
        return $this->fetch();
    }

    public function edit()
    {
        $params = $this->param();
        $res = User::where('id',$params['id'])->update(array('lable'=>1));
        if($res){
            $this->success('设置成功');
        }else{
            $this->error('设置失败');
        }
    }

    //护肤日记
    public function diary()
    {
        $params = $this->param();
        if(isset($params['nickname'])){
            $this->where[] = ['t2.nickname','like','%'.$params['nickname'].'%'];
        }
        $list = Diary::alias('t1')
            ->join('app_user t2','t2.id = t1.uid')
            ->field('t1.*,t2.nickname')
            ->where($this->where)->paginate(10,false,
            ['query'=>$params])->each(function($item, $key){
            $item['content'] = mb_substr($item['content'], 0, 20);
            $item['img'] = json_decode($item['img']);
            return $item;
        });
        $this->assign('list',$list);
        $list->appends($params);  //分页条件
        $this->assign('page', $list->render());
        return $this->fetch();
    }
    public function diaryview()
    {
        $params = $this->param();
        $info = Diary::get($params['id']);
        $info['img'] = json_decode($info['img']);
        $info['nickname'] = User::where('id',$info['uid'])->value('nickname');
        $this->assign('info',$info);
        return $this->fetch();
    }

    //店员管理
    public function clerk()
    {
        $params = $this->param();
        $where = array();
        if(isset($params['username'])){
            $where[] = ['t2.username','like','%'.$params['username'].'%'];
        }

        //多表联查 有重复字段  重新查询赋值
        $sessionAdminId = session('ADMIN_ID');
        if($sessionAdminId !== 1){
            //说明是店铺登陆
            $find = Shop::where('admin_id',$sessionAdminId)->find();
            $where[] = ['t2.shop_id','eq',$find['id']];
        }
        $list = User::alias('t1')
            ->join('app_clerk t2','t2.uid = t1.id')
            ->join('app_shop t3','t3.id = t2.shop_id')
            ->field('t1.*,t2.username,t3.name as shopname,t2.status,t2.id as clerk_id')
            ->where($where)
            ->paginate(10);
        $this->assign('list',$list);
        $list->appends($params);  //分页条件
        $this->assign('page', $list->render());
        return $this->fetch();
    }
    //店员审核
    public function clerkedit()
    {
        $params = $this->param();
        $res = Clerk::update($params);
        if($res){
            $this->success('成功');
        }else{
            $this->error('失败');
        }
    }
}
